#include "FiveWin.ch"

static lRegistered := .f.

#define BAR_HEIGHT 28

//----------------------------------------------------------------------------//

CLASS TBar FROM TControl

   DATA   aButtons, nGroups

   METHOD New( oWnd ) CONSTRUCTOR
   METHOD Adjust()
   METHOD Add( cBitmap, cMessage, bAction, lNewGroup )
   METHOD Click( hCtrl )
   METHOD HandleEvent( nMsg, nWParam, nLParam )
   METHOD SetMsg( cText )

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( oWnd ) CLASS TBar

   local oRect := oWnd:GetCliRect()

   ::nStyle   = nOR( WS_BORDER, WS_CHILD, WS_VISIBLE )
   ::aButtons = {}
   ::nGroups  = 0
   ::oWnd     = oWnd
   ::nTop     = -1
   ::nLeft    = -1
   ::nBottom  = BAR_HEIGHT
   ::nRight   = oRect:nRight
   ::nId      = ::GetNewId()

   if ! lRegistered
      ::Register( nOR( CS_VREDRAW, CS_HREDRAW, CS_GLOBALCLASS ) )
      lRegistered = .t.
   endif

   ::Create()

return Self

//----------------------------------------------------------------------------//

METHOD Add( cBitmap, cMessage, bAction, lNewGroup ) CLASS TBar

   DEFAULT lNewGroup := .f.

   if lNewGroup
      ::nGroups++
   endif

   AAdd( ::aButtons,;
         TBtnBmp():New( -1, ( Len( ::aButtons ) * ( BAR_HEIGHT + 1 ) ) + ;
                        ::nGroups * 15,;
                        30, 30, cBitmap, bAction, Self, cMessage ) )

return ATail( ::aButtons )

//----------------------------------------------------------------------------//

METHOD Click( hCtrl ) CLASS TBar

   local nAt := AScan( ::aButtons, { | oBtnBar | oBtnBar:hWnd == hCtrl } )

   if nAt != 0
      ::aButtons[ nAt ]:Click()
   endif

return

//----------------------------------------------------------------------------//

METHOD Adjust()  CLASS TBar
     
     local oRect := ::oWnd:GetCliRect()

     ::SetCoors( TRect():New( -1, -1, BAR_HEIGHT, oRect:nRight ) )

Return nil

//----------------------------------------------------------------------------//

METHOD HandleEvent( nMsg, nWParam, nLParam )    CLASS TBar

     if nMsg == WM_LBUTTONDOWN
        ::Click( nWParam )
     elseif nMsg == WM_MOUSEMOVE
        ::SetMsg( "" )
        return 0
     else
        RETURN Super:HandleEvent( nMsg, nWParam, nLParam )
     endif

return nil

//----------------------------------------------------------------------------//

METHOD SetMsg( cText ) CLASS TBar

   if ::oWnd != nil
      ::oWnd:SetMsg( cText )
   endif

return

//----------------------------------------------------------------------------//
